package com.neepu.controller;


import com.neepu.pojo.Dutresult;
import com.neepu.pojo.LogInfo;
import com.neepu.pojo.Scourse;
import com.neepu.pojo.User;
import com.neepu.service.*;
import com.neepu.util.annotation.ArchivesLog;
import com.neepu.util.common.Constants;
import com.neepu.util.common.SessionSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;//接口实现类对象

    @Autowired
    private LogInfoService logInfoService;

    @Autowired
    private PageViewService pageViewService;

    @Autowired
    private InfoManagerService infoManagerService;

    @Autowired
    private DutresultService dutresultService;

    @RequestMapping(value="/loginForm")
    public String loginForm(){
        return "loginForm";
    }

//  登陆成功挑战系统首页
    @ArchivesLog(operteContent = "用户登录")
    @RequestMapping("/login")
    public String login(){
        return "index";
    }

    @ArchivesLog(operteContent = "跳转欢迎页面")
    @RequestMapping("/welcome")
    public String welcome(Model model,HttpServletRequest request){
        //获取session
        HttpSession session = request.getSession();
        //获取session的用户信息
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        if (user.getUserType()==2){
            // 使用学号去选课表查询
            List<Scourse> list = infoManagerService.getTeacherByStudentIdAndStatus(user.getUserID(),1);
            List<Dutresult> list1 = dutresultService.getProgrammeByStudentIdAndStatus(user.getUserID(),1);
            if (list.size()!=0 || list1.size()!=0){
                model.addAttribute("message","你有未完成的评价，请前往评价管理进行评价！");
            }
        }
        int page_view = pageViewService.getPageView().getPage_view();
        model.addAttribute("page_view",page_view);
        return "welcome";
    }

    @RequestMapping(value = "/tologin" )
    public ModelAndView tologin(String loginname, String password, Integer role, ModelAndView mv, HttpSession session, HttpServletRequest request){
        User user = userService.login(loginname,password,role);
        if (user != null) {
            session.setAttribute(Constants.USER_SESSION,user);
            String sessionID = request.getRequestedSessionId();
            SessionSave.getSessionIdSave().put(loginname, sessionID);
            // 添加访问量
            int view = pageViewService.getPageView().getPage_view();
            pageViewService.addPageView(view+1);
            // 如果是学生登陆的话，查询是否已经完成了课程评价，如果没有进行提示
            mv.setViewName("forward:login");
            return mv;
        }
        mv.addObject("message","你输入的信息有误，请重新输入");
        mv.setViewName("forward:loginForm");
        return mv;
    }

    @ArchivesLog(operteContent = "查看日志")
    @RequestMapping(value = "/log")
    public ModelAndView log(ModelAndView mv,String content){

        if (content!=null&&!content.equals("")){
            List<LogInfo> list = logInfoService.findAllLogInfoByContent(content);
            mv.addObject("list",list);
            mv.setViewName("uservisit/list");
            return mv;
        }else{
            List<LogInfo> list = logInfoService.findAllLogInfo();
            mv.addObject("list",list);
            mv.setViewName("uservisit/list");
            return mv;
        }

    }

    @ArchivesLog(operteContent = "删除日志")
    @RequestMapping(value = "/dellog")
    public String dellog(Integer id){
        logInfoService.deleteLogById(id);
        return "forward:log";
    }

    @ArchivesLog(operteContent = "查看网站信息")
    @RequestMapping("website/info")
    public String website(Model model,HttpServletRequest request){
        //获取session
        HttpSession session = request.getSession();
        //获取session的用户信息
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        if (user.getUserType()==2){
            // 使用学号去选课表查询
            List<Scourse> list = infoManagerService.getTeacherByStudentIdAndStatus(user.getUserID(),1);
            List<Dutresult> list1 = dutresultService.getProgrammeByStudentIdAndStatus(user.getUserID(),1);
            if (list.size()!=0 || list1.size()!=0){
                model.addAttribute("message","你有未完成的评价，请前往评价管理进行评价！");
            }
        }
        int page_view = pageViewService.getPageView().getPage_view();
        model.addAttribute("page_view",page_view);
        return "welcome";
    }

}
